<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false" %>
<%--
  Created by IntelliJ IDEA.
  User: gz
  Date: 2025/03/26
  Time: 22:07
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <script type="text/javascript" src="script/jquery-3.7.1.min.js"></script>
    <script>
        $(function (){
            $("#getJson").click(function (){
                var url=this.href;
                var args={"time":new Date()};//防止缓存，
                $.post(
                    url,
                    args,
                    function (data){ //这里data就是返回的json格式数据
                        console.log("data= ",data)
                    },
                    "json"
                )
                return false;
            })

            $("input[type='submit']").click(function (){
                var url=$("#form").attr("action");
                var username = $("#username").val();
                var age = $("#age").val();
                //这里要将json转为字符串
                var args=JSON.stringify({"username":username,"age":age});
                $.ajax({
                    url:url,
                    data:args, //因为是发送的json字符串，所以后端那里要用@RequestBody 标注参数,否则返回为空
                    type:"POST",
                    success:function(data){ //因为用了@ResonseBody,返回的是json
                        console.log("返回的data= ",data)
                    },
                    //指定发送数据时的编码和格式
                    contentType:"application/json;charset=utf-8"
                })
                return false;
            })
        })
    </script>
</head>
<body>
<a href="json/dog" id="getJson">点击获取json数据</a> <br> <br>

<form action="json/user" method="post" id="form">
用户名<input type="text" id="username"> <br><br>
年 龄<input type="text" id="age">
    <input type="submit" value="提交">
</form>
<br><br>
<a href="download">点击下载文件</a>
</body>
</html>
